package com.ufgov.agcfs.baseData.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import uap.iweb.entity.DataTable;
import uap.iweb.exception.WebRuntimeException;
import uap.iweb.icontext.IWebViewContext;

import com.ufgov.agcfs.baseData.entity.BookType;
import com.ufgov.agcfs.baseData.service.BookTypeService;
import com.ufgov.ip.base.service.IPDataTableService;

@Controller("baseData.bookType.bookTypeController")
@Scope("prototype")
@RequestMapping(value = "/baseData/bookType")
public class BookTypeController {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private BookTypeService bookService;
	
	@Autowired 
	IPDataTableService iPDataTableService;
	
	DataTable<BookType> bookTypeDataTable;
	
	public void loadBookType() {
		try {
			Map<String, Object> searchParams = new HashMap<String, Object>();
			searchParams = iPDataTableService.createSearchParamsStartingWith("search_",bookTypeDataTable);
			String nd = (String) searchParams.get("nd");
			List<BookType> bookList=bookService.findByNd(nd);
			bookTypeDataTable.remove(bookTypeDataTable.getAllRow());
			bookTypeDataTable.set(bookList.toArray(new BookType[0]));
			IWebViewContext.getResponse().write("true");
		} catch (Exception e) {
			logger.error(e.getMessage());
			IWebViewContext.getResponse().write("false");
			throw new WebRuntimeException("查询数据失败!");
		}
	}
	
	@RequestMapping(method = RequestMethod.GET,value="getAllNd")
	public @ResponseBody List<String> getAllnd(){
		List<String> ndList = new ArrayList<String>();
		try {
			ndList = bookService.getAllNd();
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return ndList;		
	}
	
	@RequestMapping(method = RequestMethod.GET,value="getBookListByNd")
	public @ResponseBody List<BookType> getBookListByNd(@RequestParam(value="nd")String nd){
		try {
			List<BookType> bookList=bookService.findByNd(nd);
			return bookList;
		} catch (Exception e) {
			logger.error(e.getMessage());
			IWebViewContext.getResponse().write("false");
			throw new WebRuntimeException("查询数据失败!");
		}		
	}
	
	@RequestMapping(method = RequestMethod.POST,value="deleteBookType")
	public @ResponseBody Map<String, String> deleteBookType(@RequestParam(value="books")String books){
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("result", "success");
		resultMap.put("reason", "删除成功！");
		try {
			JSONArray jsonAry = JSONArray.fromObject(books);
			bookService.deleteBookType(jsonAry,resultMap);
		} catch (Exception e) {
			logger.error(e.getMessage());
			resultMap.put("result", "fail");
			resultMap.put("reason", "删除失败，请稍后重试！");
		}
		return resultMap;
	}
 
	@RequestMapping(method = RequestMethod.POST,value="saveBookType")
	public  @ResponseBody Map<String, String> saveBookType(@RequestBody BookType bookType){
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("result", "success");
		resultMap.put("reason", "保存成功！");
		try {
			bookService.saveBookType(bookType,resultMap);
		} catch (Exception e) {
			logger.error(e.getMessage());
			resultMap.put("result", "fail");
			resultMap.put("reason", "保存失败，请稍后重试！");
		}
		return resultMap;
	}
}
